package common;
import org.apache.commons.lang.StringUtils;

/**
 * created by sonite on 2018/8/3
 */
public  class StringTransTools {
    public static String parseString(String source,String startStr,String splitChar) {
        String rep = source.replace("\"", "");
        //不满足转化规则的直接返回
        if (startStr==null||splitChar==null||rep.indexOf(startStr) < 0) {
            return "";
        }

        String tailStr = rep.substring(rep.indexOf(startStr) + 1);
        if(StringUtils.isEmpty(tailStr)){
            return "";
        }
           StringBuffer buffer = new StringBuffer();
            String[] splitStr = tailStr.split(splitChar);
            String temp = "";
            for (int i = splitStr.length - 1; i >= 0; i--) {
                if (splitStr[i].indexOf("=") > 0) {
                    if (!StringUtils.isEmpty(temp)) {
                        temp = splitStr[i] + " " + temp;
                        splitStr[i] = temp;
                        temp = "";
                    }

                    splitStr[i] = splitStr[i].substring(splitStr[i].indexOf("=") + 1);
                    if (StringUtils.isEmpty(splitStr[i])) {
                        splitStr[i] = "null";
                    }

                } else {
                    temp = splitStr[i] + " " + temp;
                    splitStr[i] = "";
                }
            }
                for (String t : splitStr) {
                    if (!StringUtils.isEmpty(t)) {
                        if ("null".equals(t)) {
                            buffer.append("|");
                            continue;
                        }
                        buffer.append(t.trim()).append("|");
                    }

                }


            return buffer.substring(0, buffer.lastIndexOf("|"));
        }

}
